﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyCommunity.Models;
using System.Web.Security;
using MyCommunity.DataAccess;

public class EventDAL : IEventDAL
{
    //datacontext for entity framework
    private readonly MyCommunityDataContext _db;

    public EventDAL()
    {
        _db = new MyCommunityDataContext();
    }

    public Event GetEvent(int eventId)
    {
        return _db.Event.FirstOrDefault(i => i.Id == eventId);
    }

    public List<Event> GetAllEvents()
    {
        return _db.Event.ToList().OrderByDescending(i => i.StartDate).ToList();
    }

    public List<Event> FindEvents(String searchString)
    {
        return _db.Event.Where(s => s.Title.Contains(searchString)).ToList();
    }

    public void Add(Event eventNew)
    {
        _db.Event.InsertOnSubmit(eventNew);
    }

    public void Remove(Event eventOld)
    {
        _db.Event.DeleteOnSubmit(eventOld);
    }

    public void Save()
    {
        _db.SubmitChanges();
    }
}